import React, {Component} from 'react';
import './index.less';
import {
    toast,
    promptBox,
    cacheTools
} from 'nc-lightapp-front';
import QueryCtrtInfAction from "./functions/QueryCtrtInfAction";
// 签订=1,续签=2,变更=3,解除=4,终止=5
let pagecode = {
    '1': {
        appCode: '60110303',
        formId: 'make'
    },
    '2': {
        appCode: '60110307',
        formId: 'extend'
    },
    '3': {
        appCode: '60110305',
        formId: 'change'
    },
    '4': {
        appCode: '60110309',
        formId: 'release'
    },
    '5': {
        appCode: '60110311',
        formId: 'finish'
    }
};

class DetailPage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            json: props.json,
        };
    }

    componentWillReceiveProps(nextprops) {
        let nextjson = JSON.stringify(nextprops.json);
        let thisjson = JSON.stringify(this.props.json);
        if (nextjson !== thisjson) {
            this.setState({
                json: nextprops.json
            })
        }
    }

    componentDidMount() {

    }

    /**
     * 获取详情页数据
     */
    getDetailData = (callback) => {
        let conttype = this.props.conttype;
        this.areaCode = pagecode[conttype].formId;
        QueryCtrtInfAction(this.props.pk_psndoc_sub, this.areaCode).then(res => {
            if(!res.success)return;
            if(callback && typeof callback === 'function'){
                callback();
            }
            this.setDetailMeta(res.data);
        })
    };

    setDetailMeta(data) {
        /*let meta = this.props.meta.getMeta() || {};
        meta[''] = data;
        this.props.meta.setMeta(meta, () => {
            //设置表单数据
        });*/
        this.props.form.setAllFormValue({[this.areaCode]: {rows: data[this.areaCode].rows}})
    }

    /**
     * 更新state对象
     * @param data
     * @param callback
     */
    updateState(data, callback = () => {
    }) {
        this.setState(data, callback);
    }

    /**
     * 清空表格
     */
    emptyRightTable() {
        this.props.updateState({
            pk_psndoc_sub: null,

        });
        this.props.editTable.setTableData(tableId, {rows: []});
        this.props.editTable.setTableData(tableId2, {rows: []});
    }

    render() {
        const {form} = this.props;
        const {createForm} = form;
        let formId = null;
        if (this.props.conttype) {
            formId = pagecode[this.props.conttype].formId
        }
        return (
            <div className="detail-page">
                {formId && createForm(formId)}
            </div>
        );
    }
}

export default DetailPage;
